#include "xmlelement.h"

XMLElement::XMLElement(ifstream *f){
    m_Type  =   ElementNode;
    LoadTag(f);
    LoadContent(f);
}

void XMLElement::LoadTag(ifstream * f){
    stringstream str;
    char c;
    *f>> c;
    while (c !='>' && !f->eof()){
        str << c;
        *f>> c;
    }
    str >> tagName;
}

void XMLElement::LoadContent(ifstream *f){
    char c;
    bool end = false;
    while (!end && !f->eof()){
        *f>> c;
        if(c=='<'){
            if(f->peek()!='/'){
                childs.push_back(new XMLElement(f));
            }else{
                f->get();
                *f >> c;
                stringstream textnode;
                while (c !='>' && !f->eof()){
                    textnode<< c;
                    *f >> c;
                }
                end=true;
            }
        }else if( (c!=' ') && (c!='   ' )  && (c!='\n')){
            stringstream textnode;

            while (c !='<') {
                textnode<< c;
                *f >> c;
            }
            f->unget();
            childs.push_back(new XMLTextNode( textnode.str()));
        }
    }
}
